/* pathfinder.d: 智能路径查找器


   */
module windump.pathfinder;

import std.regexp;
import windump.registry;
import std.string;

class PathFinder
{
    private RegExp m_regex;
    private string[] m_paths;

    public this(string regex)
    {
        m_regex = new RegExp(regex, "i"); //忽略大小写
        tryUninstallString();
    }

    public int opApply(int delegate(ref string path) dg)
    {
        int result = 0;

        foreach(path; m_paths)
        {
            result = dg(path);
            if (result) break;
        }

        return result;
    }

    //查找注册表里的反安装路径
    private string tryUninstallString()
    {
        const string UninstallPath = `SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall`;

        Key k = Registry.localMachine.openSubkey(UninstallPath);

        scope(exit) k.close();

        auto subkeys = k.subkeyNames; //所有的反安装程序名称
        foreach(subkeyName; subkeys)
        {
            Key subkey = Registry.localMachine.openSubkey(UninstallPath ~ `\` ~ subkeyName);
            string path = k.getValue("UninstallString").value_SZ();

            scope(exit) subkey.close();
        }


        string path = k.getValue("UninstallString").value_SZ();
		path = tolower(path);
        int pos = std.string.find(path, "\\unwise.exe");
		if(pos >= 0) 
			path = path[0 .. pos];

        return path;
    }

}
